package com.tao.lab.event.dispath;

import android.os.Looper;

import com.tao.lab.Poster;
import com.tao.lab.event.dispath.poster.AsyncPoster;
import com.tao.lab.event.dispath.poster.PendingPost;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ProjectName: lab
 * @Package: com.tao.lab.event.dispath
 * @ClassName: PostUtil
 * @Description: java类作用描述
 * @Author: wangxf
 * @CreateDate: 2022/5/6 11:55
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/5/6 11:55
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class EventBus {
    private MainThreadSupport mainThreadSupport = getMainThreadSupport();
    private BackgroundPoster backgroundPoster = new BackgroundPoster(this);
    private final Poster mainThreadPoster = mainThreadSupport.createPoster(this);
    private static final ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();
    private AsyncPoster asyncPoster = new AsyncPoster(this);

    private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case POSTING:
                this.invokeSubscriber(subscription, event);
                break;
            case MAIN:
                if (isMainThread) {
                    this.invokeSubscriber(subscription, event);
                } else {
                    this.mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case MAIN_ORDERED:
                if (this.mainThreadPoster != null) {
                    this.mainThreadPoster.enqueue(subscription, event);
                } else {
                    this.invokeSubscriber(subscription, event);
                }
                break;
            case BACKGROUND:
                if (isMainThread) {
                    this.backgroundPoster.enqueue(subscription, event);
                } else {
                    this.invokeSubscriber(subscription, event);
                }
                break;
            case ASYNC:
                this.asyncPoster.enqueue(subscription, event);
                break;
            default:
                throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
        }

    }


    public void invokeSubscriber(PendingPost pendingPost) {
        Object event = pendingPost.event;
        Subscription subscription = pendingPost.subscription;
        PendingPost.releasePendingPost(pendingPost);
        if (subscription.active) {
            this.invokeSubscriber(subscription, event);
        }

    }

    void invokeSubscriber(Subscription subscription, Object event) {
        try {
            subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
        } catch (InvocationTargetException var4) {
        } catch (IllegalAccessException var5) {
            throw new IllegalStateException("Unexpected exception", var5);
        }

    }

    MainThreadSupport getMainThreadSupport() {
        if (this.mainThreadSupport != null) {
            return this.mainThreadSupport;
        } else if (true) {
            Object looperOrNull = this.getAndroidMainLooperOrNull();
            return new MainThreadSupport.AndroidHandlerMainThreadSupport((Looper) looperOrNull);
        } else {
            return null;
        }
    }

    public ExecutorService getExecutorService() {
        return DEFAULT_EXECUTOR_SERVICE;
    }

    Object getAndroidMainLooperOrNull() {
        try {
            return Looper.getMainLooper();
        } catch (RuntimeException var2) {
            return null;
        }
    }
}
